package com.team.lostProperty.controller;

import com.team.lostProperty.entity.*;
import com.team.lostProperty.service.TypeService;
import com.team.lostProperty.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author duzq
 * @date 2020/07/22 18:33
 **/
@RestController
@RequestMapping("/type")
public class TypeController {

    private IdWorker idWorker = new IdWorker(1, 1, 1);

    @Autowired
    private TypeService typeService;

    Result result = new Result();

    /**
     * 获取类别
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/getType")
    private Result getType(){
        List<Type> typeList = typeService.getType();
        if (typeList.size()>=0){
            result.setCode(0);
            result.setMsg("成功");
            result.setCount(typeService.getAllCount());
            result.setData(typeList);
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setCount(0);
            result.setData("");
        }
        return result;
    }

    /**
     * 根据条件查询类别
     * @param request
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/selectType")
    private Result selectComments(HttpServletRequest request){
        Type type = new Type();
        Admin admin = new Admin();

        if (!request.getParameter("id").equals("")){
            type.setTypeId(Integer.valueOf(request.getParameter("id")));
        }
        admin.setNickName(request.getParameter("author"));
        type.setAdmin(admin);
        type.setName(request.getParameter("name"));

        List<Type> typeList = typeService.selectType(type);
        if (typeList.size() >= 0){
            result.setCode(0);
            result.setMsg("成功");
            result.setData(typeList);
            result.setCount(typeService.getAllCount());
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
            result.setCount(0);
        }
        return result;
    }

    /**
     *根据ID删除类别
     * @param ids
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/deleteType")
    private Result deleteType(String ids){
        int i = 0;
        if (ids != null && !ids.equals("")) {
            String[] str = ids.split(",");
            for (String id : str) {
                if (id != null && !id.equals("")) {
                    i = typeService.deleteType(Long.valueOf(id));
                }
            }
        }
        if (i > 0){
            result.setCode(0);
            result.setMsg("成功");
            result.setData("");
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
        }
        return result;
    }

    /**
     * 修改类别信息
     * @param request
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/updateType")
    private Result updateType(HttpServletRequest request){
        Type type = new Type();
        type.setTypeId(Long.valueOf(request.getParameter("id")));
        type.setName(request.getParameter("name"));
        type.setRemarks(request.getParameter("remarks"));
        int i = typeService.updateType(type);
        if (i > 0){
            result.setCode(0);
            result.setMsg("成功");
            result.setData("");
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
        }
        return result;
    }

    /**
     * 添加类型信息
     * @param request
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/insertType")
    private Result insertType(HttpServletRequest request){
        Type type = new Type();
        Admin admin = new Admin();
        type.setTypeId(idWorker.nextId());
        type.setName(request.getParameter("name"));
        type.setRemarks(request.getParameter("remarks"));
        admin.setAdminId(1);
        type.setAdmin(admin);
        int i = typeService.insertType(type);

        if (i > 0){
            result.setCode(0);
            result.setMsg("成功");
            result.setData("");
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
        }
        return result;
    }
}
